package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Factory;
import com.ruoyi.system.service.IFactoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工厂Controller
 * 
 * @author ruoyi
 * @date 2025-06-23
 */
@Controller
@RequestMapping("/system/factory")
public class FactoryController extends BaseController
{
    private String prefix = "system/factory";

    @Autowired
    private IFactoryService factoryService;

    @RequiresPermissions("system:factory:view")
    @GetMapping()
    public String factory()
    {
        return prefix + "/factory";
    }

    /**
     * 查询工厂列表
     */
    @RequiresPermissions("system:factory:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Factory factory)
    {
        startPage();
        List<Factory> list = factoryService.selectFactoryList(factory);
        
        return getDataTable(list);
    }

    /**
     * 导出工厂列表
     */
    @RequiresPermissions("system:factory:export")
    @Log(title = "工厂", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Factory factory)
    {
        List<Factory> list = factoryService.selectFactoryList(factory);
        ExcelUtil<Factory> util = new ExcelUtil<Factory>(Factory.class);
        return util.exportExcel(list, "工厂数据");
    }

    /**
     * 新增工厂
     */
    @RequiresPermissions("system:factory:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存工厂
     */
    @RequiresPermissions("system:factory:add")
    @Log(title = "工厂", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Factory factory)
    {
        return toAjax(factoryService.insertFactory(factory));
    }

    /**
     * 修改工厂
     */
    @RequiresPermissions("system:factory:edit")
    @GetMapping("/edit/{factoryId}")
    public String edit(@PathVariable("factoryId") Long factoryId, ModelMap mmap)
    {
        Factory factory = factoryService.selectFactoryByFactoryId(factoryId);
        mmap.put("factory", factory);
        return prefix + "/edit";
    }

    /**
     * 修改保存工厂
     */
    @RequiresPermissions("system:factory:edit")
    @Log(title = "工厂", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Factory factory)
    {
        return toAjax(factoryService.updateFactory(factory));
    }

    /**
     * 删除工厂
     */
    @RequiresPermissions("system:factory:remove")
    @Log(title = "工厂", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(factoryService.deleteFactoryByFactoryIds(ids));
    }
}
